package com.huike.review;

import io.jsonwebtoken.*;
import sun.java2d.pipe.SpanIterator;

import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.Locale;
import java.util.UUID;

public class Test {

    private long time = 1000 * 60 * 60 * 24;
    //签名信息
    private String signature = "admin";

    @org.junit.Test
    public void jwt(){
        JwtBuilder builder = Jwts.builder();
        String jwtToken = builder
                //header
                .setHeaderParam("typ", "JWT")
                .setHeaderParam("alg","Hs256")
                //payload
                .claim("username","tom")
                .claim("role","admin")
                //主题
                .setSubject("admin-test")
                //有效时间
                .setExpiration(new Date(System.currentTimeMillis() + time))
                .setId(UUID.randomUUID().toString())
                //signature
                .signWith(SignatureAlgorithm.HS256,signature)
                .compact();

        System.out.println(jwtToken);
    }

    //解密
    @org.junit.Test
    public void parse(){
        String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6InRvbSIsInJvbGUiOiJhZG1pbiIsInN1YiI6ImFkbWluLXRlc3QiLCJleHAiOjE2NjE1OTExNDUsImp0aSI6IjgwZjNlYmEwLTY1NDAtNDIxZS04NDhhLTBmYmQ1NDAwYTU4YyJ9.Mwa7CiuelLNyIMYIBuddm7SaDKdV-_yqHzZzHGwQNJs";
        JwtParser parser = Jwts.parser();
        //通过签名对token进行解析
        Jws<Claims> claimsJws = parser.setSigningKey(signature).parseClaimsJws(token);
        //上面封装的数据全部在claims里面了
        Claims claims = claimsJws.getBody();
        System.out.println(claims.get("username"));
        System.out.println(claims.get("role"));
        System.out.println(claims.getId());
        System.out.println(claims.getSubject());
        System.out.println(claims.getExpiration());
    }


    @org.junit.Test
    public void stringTest(){
        String str = "123456789";
        String str2 = "abcdefg";
        String str1 = " 123456789 ";
        //int i = str.indexOf('4');
        //System.out.println(i);

        //char c = str.charAt(2);
        //System.out.println(c);

        //String a = str.replace("1", "a");
        //System.out.println(a);

        //System.out.println(str1.trim());

        //byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
        //System.out.println(bytes);

        //String s1 = str2.toUpperCase(Locale.ROOT);
        //System.out.println(s1);

        //String s = s1.toLowerCase(Locale.ROOT);
        //System.out.println(s);

        //String substring = str.substring(0,2);
        //System.out.println(substring);

        Date date = new Date();
        System.out.println(date);
    }





}
